package com.example.jedisdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-11-10
 * Time: 23:03
 */

// 后续 redis 测试的各种方法，都通过这个 Controller 提供的 http 接口触发
@RestController
public class MyController {

    // 该类是 RedisTemplate 的子类，仅能处理文本数据
    // RedisTemplate 还能处理二进制数据
    @Autowired
    private StringRedisTemplate redisTemplate;


    @GetMapping("/testString")
    @ResponseBody
    public String testString() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        // 进一步封装，分成几类
        redisTemplate.opsForValue();// 操纵字符串
        redisTemplate.opsForZSet();// 操纵 ZSet 对象
        redisTemplate.opsForSet();// 操纵 Set 对象
        redisTemplate.opsForHash();// 操纵 Hash 对象
        redisTemplate.opsForList();// 操纵 List 对象


        ValueOperations<String, String> jedis1 = redisTemplate.opsForValue();
        jedis1.set("key", "111");
        jedis1.set("key2", "222");
        jedis1.set("key3", "333");

        String value = jedis1.get("key");
        System.out.println("value:" + value);

        return "OK";
    }


    @GetMapping("/testList")
    @ResponseBody
    public String testList() {
        // 先清除数据库
        // redisTemplate 没有直接提供方法，而是提供了 execute 方法，
        // connection 里提供的就是原生的 redis 命令
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            // 该回调方法必须返回数据
            // 然后作为 execute 本身的返回值
            return null;
        });

        // left == lpush 头插
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "222");
        redisTemplate.opsForList().leftPush("key", "333");

        // 尾删
        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);

        return "OK";
    }



    @GetMapping("/testSet")
    @ResponseBody
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists:" + exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count:" + count);

        count = redisTemplate.opsForSet().remove("key", "222", "333");
        System.out.println("count:" + count);

        result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);

        return "ok";
    }



    @GetMapping("/testHash")
    @ResponseBody
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "222");
        redisTemplate.opsForHash().put("key", "f3", "333");
        redisTemplate.opsForHash().put("key", "f4", "444");

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value:" + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists:" + exists);

        redisTemplate.opsForHash().delete("key", "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size:" + size);

        return "ok";
    }


    @GetMapping("/testZSet")
    @ResponseBody
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key", "zhangsan", 10);
        redisTemplate.opsForZSet().add("key", "lisi", 20);
        redisTemplate.opsForZSet().add("key", "wangwu", 30);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members:" + members);

        Set<ZSetOperations.TypedTuple<String>> memberWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("memberWithScore:" + memberWithScore);

        Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
        System.out.println("score:" + score);

        redisTemplate.opsForZSet().remove("key", "zhangsan");
        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("count:" + size);

        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank:" + rank);


        return "ok";
    }



}
